home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / umich / network / ka9q / ka9q_src.arc / TCPCMD.C < prev    next >
C/C++ Source or Header  |  1988-07-28  |  6KB  |  298 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "timer.h"
  4. #include "mbuf.h"
  5. #include "netuser.h"
  6. #include "internet.h"
  7. #include "tcp.h"
  8. #include "cmdparse.h"
  9.  
  10. /* TCP connection states */
  11. char *tcpstates[] = {
  12.     "Closed",
  13.     "Listen",
  14.     "SYN sent",
  15.     "SYN received",
  16.     "Established",
  17.     "FIN wait 1",
  18.     "FIN wait 2",
  19.     "Close wait",
  20.     "Closing",
  21.     "Last ACK",
  22.     "Time wait"
  23. };
  24.  
  25. /* TCP closing reasons */
  26. char *reasons[] = {
  27.     "Normal",
  28.     "Reset",
  29.     "Timeout",
  30.     "ICMP"
  31. };
  32. /* TCP subcommand table */
  33. int domss(),doirtt(),dortt(),dotcpstat(),dowindow(),dotcpkick(),dotcpreset();
  34. struct cmds tcpcmds[] = {
  35.     "irtt",     doirtt,     0,    NULLCHAR,    NULLCHAR,
  36.     "kick",     dotcpkick,    2,    "tcp kick <tcb>",
  37.         NULLCHAR,
  38.     "mss",        domss,        0,    NULLCHAR,    NULLCHAR,
  39.     "reset",    dotcpreset,    2,    "tcp reset <tcb>",
  40.         NULLCHAR,
  41.     "rtt",        dortt,        3,    "tcp rtt <tcb> <val>",
  42.         NULLCHAR,
  43.     "status",    dotcpstat,    0,    NULLCHAR,    NULLCHAR,
  44.     "window",    dowindow,    0,    NULLCHAR,    NULLCHAR,
  45.     NULLCHAR,    NULLFP,     0,
  46.         "tcp subcommands: irtt kick mss reset rtt status window",
  47.         NULLCHAR,
  48. };
  49. int
  50. dotcp(argc,argv)
  51. int argc;
  52. char *argv[];
  53. {
  54.     return subcmd(tcpcmds,argc,argv);
  55. }
  56.  
  57. /* Eliminate a TCP connection */
  58. static int
  59. dotcpreset(argc,argv)
  60. int argc;
  61. char *argv[];
  62. {
  63.     register struct tcb *tcb;
  64.     extern char notval[];
  65.     long htol();
  66.  
  67.     tcb = (struct tcb *)htol(argv[1]);
  68.     if(!tcpval(tcb)){
  69.         printf(notval);
  70.         return 1;
  71.     }
  72.     close_self(tcb,RESET);
  73.     return 0;
  74. }
  75.  
  76. /* Set initial round trip time for new connections */
  77. static int
  78. doirtt(argc,argv)
  79. int argc;
  80. char *argv[];
  81. {
  82.     if(argc < 2)
  83.         printf("%d\n",tcp_irtt);
  84.     else
  85.         tcp_irtt = atoi(argv[1]);
  86.     return 0;
  87. }
  88.  
  89. /* Set smoothed round trip time for specified TCB */
  90. static int
  91. dortt(argc,argv)
  92. int argc;
  93. char *argv[];
  94. {
  95.     register struct tcb *tcb;
  96.     extern char notval[];
  97.     long htol(),atol();
  98.  
  99.     tcb = (struct tcb *)htol(argv[1]);
  100.     if(!tcpval(tcb)){
  101.         printf(notval);
  102.         return 1;
  103.     }
  104.     tcb->srtt = atol(argv[2]);
  105.     return 0;
  106. }
  107.  
  108. /* Force a retransmission */
  109. static int
  110. dotcpkick(argc,argv)
  111. int argc;
  112. char *argv[];
  113. {
  114.     register struct tcb *tcb;
  115.     extern char notval[];
  116.     long htol();
  117.  
  118.     tcb = (struct tcb *)htol(argv[1]);
  119.     if(kick_tcp(tcb) == -1){
  120.         printf(notval);
  121.         return 1;
  122.     }
  123.     return 0;
  124. }
  125.  
  126. /* Set default maximum segment size */
  127. static int
  128. domss(argc,argv)
  129. int argc;
  130. char *argv[];
  131. {
  132.     if(argc < 2)
  133.         printf("%d\n",tcp_mss);
  134.     else
  135.         tcp_mss = atoi(argv[1]);
  136.     return 0;
  137. }
  138.  
  139. /* Set default window size */
  140. static int
  141. dowindow(argc,argv)
  142. int argc;
  143. char *argv[];
  144. {
  145.     if(argc < 2)
  146.         printf("%d\n",tcp_window);
  147.     else
  148.         tcp_window = atoi(argv[1]);
  149.     return 0;
  150. }
  151.  
  152. /* Display status of TCBs */
  153. static int
  154. dotcpstat(argc,argv)
  155. int argc;
  156. char *argv[];
  157. {
  158.     long htol();
  159.     register struct tcb *tcb;
  160.     extern char notval[];
  161.  
  162.     if(argc < 2){
  163.         tstat();
  164.     } else {
  165.         tcb = (struct tcb *)htol(argv[1]);
  166.         if(tcpval(tcb))
  167.             state_tcp(tcb);
  168.         else
  169.             printf(notval);
  170.     }
  171.     return 0;
  172. }
  173.  
  174. /* Dump TCP stats and summary of all TCBs
  175.  *     &TCB Rcv-Q Snd-Q  Local socket        Remote socket           State
  176.  *     1234    0     0  xxx.xxx.xxx.xxx:xxxxx    xxx.xxx.xxx.xxx:xxxxx  Established
  177.  */
  178. static int
  179. tstat()
  180. {
  181.     register int i;
  182.     register struct tcb *tcb;
  183.     char *psocket();
  184.  
  185.     printf("conout %u conin %u reset out %u runt %u chksum err %u bdcsts %u\n",
  186.         tcp_stat.conout,tcp_stat.conin,tcp_stat.resets,tcp_stat.runt,
  187.         tcp_stat.checksum,tcp_stat.bdcsts);
  188.     printf("    &TCB Rcv-Q Snd-Q  Local socket         Remote socket        State\n");
  189.     for(i=0;i<NTCB;i++){
  190.         for(tcb=tcbs[i];tcb != NULLTCB;tcb = tcb->next){
  191.             printf("%8lx%6u%6u  ",(long)tcb,tcb->rcvcnt,tcb->sndcnt);
  192.             printf("%-23s",psocket(&tcb->conn.local));
  193.             printf("%-23s",psocket(&tcb->conn.remote));
  194.             printf("%-s",tcpstates[tcb->state]);
  195.             if(tcb->state == LISTEN && (tcb->flags & CLONE))
  196.                 printf(" (S)");
  197.             printf("\n");
  198.         }
  199.     }
  200.     fflush(stdout);
  201.     return 0;
  202. }
  203. /* Dump a TCP control block in detail */
  204. static void
  205. state_tcp(tcb)
  206. struct tcb *tcb;
  207. {
  208.     int32 sent,recvd;
  209.  
  210.     if(tcb == NULLTCB)
  211.         return;
  212.     /* Compute total data sent and received; take out SYN and FIN */
  213.     sent = tcb->snd.una - tcb->iss; /* Acknowledged data only */
  214.     recvd = tcb->rcv.nxt - tcb->irs;
  215.     switch(tcb->state){
  216.     case LISTEN:
  217.     case SYN_SENT:        /* Nothing received or acked yet */
  218.         sent = recvd = 0;    
  219.         break;
  220.     case SYN_RECEIVED:
  221.         recvd--;    /* Got SYN, no data acked yet */
  222.         sent = 0;
  223.         break;
  224.     case ESTABLISHED:    /* Got and sent SYN */
  225.     case FINWAIT1:        /* FIN not acked yet */
  226.         sent--;
  227.         recvd--;
  228.         break;
  229.     case FINWAIT2:        /* Our SYN and FIN both acked */
  230.         sent -= 2;
  231.         recvd--;
  232.         break;
  233.     case CLOSE_WAIT:    /* Got SYN and FIN, our FIN not yet acked */
  234.     case CLOSING:
  235.     case LAST_ACK:
  236.         sent--;
  237.         recvd -= 2;
  238.         break;
  239.     case TIME_WAIT:     /* Sent and received SYN/FIN, all acked */
  240.         sent -= 2;
  241.         recvd -= 2;
  242.         break;
  243.     }
  244.     printf("Local: %s",psocket(&tcb->conn.local));
  245.     printf(" Remote: %s",psocket(&tcb->conn.remote));
  246.     printf(" State: %s\n",tcpstates[tcb->state]);
  247.     printf("      Init seq      Unack     Next      WL1      WL2  Wind   MSS Queue      Total\n");
  248.     printf("Send:");
  249.     printf("%9lx",tcb->iss);
  250.     printf("%9lx",tcb->snd.una);
  251.     printf("%9lx",tcb->snd.nxt);
  252.     printf("%9lx",tcb->snd.wl1);
  253.     printf("%9lx",tcb->snd.wl2);
  254.     printf("%6u",tcb->snd.wnd);
  255.     printf("%6u",tcb->mss);
  256.     printf("%6u",tcb->sndcnt);
  257.     printf("%11lu\n",sent);
  258.  
  259.     printf("Recv:");
  260.     printf("%9lx",tcb->irs);
  261.     printf("         ");
  262.     printf("%9lx",tcb->rcv.nxt);
  263.     printf("         ");
  264.     printf("         ");
  265.     printf("%6u",tcb->rcv.wnd);
  266.     printf("      ");
  267.     printf("%6u",tcb->rcvcnt);
  268.     printf("%11lu\n",recvd);
  269.  
  270.     if(tcb->reseq != (struct reseq *)NULL){
  271.         register struct reseq *rp;
  272.  
  273.         printf("Reassembly queue:\n");
  274.         for(rp = tcb->reseq;rp != (struct reseq *)NULL; rp = rp->next){
  275.             printf("  seq x%lx %u bytes\n",rp->seg.seq,rp->length);
  276.         }
  277.     }
  278.     if(tcb->backoff > 0)
  279.         printf("Backoff %u ",tcb->backoff);
  280.     if(tcb->flags & RETRAN)
  281.         printf("Retrying ");
  282.     switch(tcb->timer.state){
  283.     case TIMER_STOP:
  284.         printf("Timer stopped ");
  285.         break;
  286.     case TIMER_RUN:
  287.         printf("Timer running (%ld/%ld ms) ",
  288.          (long)MSPTICK * (tcb->timer.start - tcb->timer.count),
  289.          (long)MSPTICK * tcb->timer.start);
  290.         break;
  291.     case TIMER_EXPIRE:
  292.         printf("Timer expired ");
  293.     }
  294.     printf("Smoothed round trip time %ld ms\n",tcb->srtt);
  295.     fflush(stdout);
  296. }
  297.  
  298.